﻿using System;
using System.Runtime.InteropServices;
using System.Windows;
using Microsoft.Office.Interop.Word;
using Application = Microsoft.Office.Interop.Word.Application;
using System.Windows.Forms.Integration;
using System.Windows.Forms; // 必须引用 Windows Forms 命名空间

namespace WPF_WordTemplateEditor_Demo
{
    public partial class MainWindow : System.Windows.Window
    {
        private Application wordApp; // Word 应用实例
        private Document wordDoc;    // 当前加载的 Word 文档
        private WindowsFormsHost wordHostControl; // WindowsFormsHost 控件

        public MainWindow()
        {
            InitializeComponent();
            InitializeWord();
        }

        /// <summary>
        /// 初始化 Word 应用
        /// </summary>
        private void InitializeWord()
        {
            try
            {
                // 创建 Word 应用对象
                wordApp = new Application
                {
                    Visible = true // 将 Word 设置为可见
                };

                // 打开模板文档
                string templatePath = @"D:\File\Desktop\Template.docx"; // 确保此路径有效
                wordDoc = wordApp.Documents.Open(templatePath);

                // 创建 Windows Forms 控件容器
                var wordControl = new WordActiveXHost();

                // 将 Word 控件嵌入到 WindowsFormsHost 中
                wordHostControl = new WindowsFormsHost
                {
                    Child = wordControl // 设置 WindowsFormsHost 的 Child
                };

                // 将 WindowsFormsHost 控件添加到 WPF 窗口
                this.wordHost.Child = wordHostControl.Child; // 直接将 Word 控件嵌入到 WPF 窗口
            }
            catch (COMException ex)
            {
                System.Windows.MessageBox.Show($"无法启动 Word 应用：{ex.Message}");
            }
        }

        /// <summary>
        /// 查找并替换 Word 中的文本
        /// </summary>
        private void FindAndReplace(string findText, string replaceText)
        {
            if (wordDoc != null)
            {
                Find find = wordApp.Selection.Find;
                find.ClearFormatting();
                find.Text = findText;
                find.Replacement.ClearFormatting();
                find.Replacement.Text = replaceText;

                object replaceAll = WdReplace.wdReplaceAll;
                find.Execute(Replace: ref replaceAll);
            }
        }

        /// <summary>
        /// 处理窗口关闭事件
        /// </summary>
        private void Window_Closed(object sender, EventArgs e)
        {
            if (wordDoc != null)
            {
                wordDoc.Close(false); // 关闭 Word 文档
                Marshal.ReleaseComObject(wordDoc); // 释放 COM 对象
                wordDoc = null;
            }

            if (wordApp != null)
            {
                wordApp.Quit(); // 退出 Word 应用
                Marshal.ReleaseComObject(wordApp); // 释放 COM 对象
                wordApp = null;
            }
        }
    }

    /// <summary>
    /// 创建嵌入的 Word 控件，继承自 AxHost
    /// </summary>
    public class WordActiveXHost : AxHost
    {
        // Word 控件的 CLSID
        private static readonly string clsid = "000209FF-0000-0000-C000-000000000046";

        public WordActiveXHost() : base(clsid)
        {
        }
    }
}
